SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		lLara
-- Create date: 21/3/2014
-- Description:	?Descripcion
-- =============================================
CREATE TRIGGER [obd.].[ActualizarFlotte]
ON [obd.].[Flotte]
AFTER UPDATE
AS 
BEGIN
	SET NOCOUNT ON;

	DECLARE curDel CURSOR LOCAL FOR 
					SELECT idFlotte, ISNULL(protocol,'n/a'), idSystem, 
						ISNULL(ecuHardware,'n/a'), ISNULL(ecuType,'n/a'), ISNULL(engineProject,'n/a'), 
						displacement, ISNULL(emissionConcept,'n/a'), ISNULL(platform,'n/a'), 
						ISNULL(power,'n/a'), ISNULL(gearbox,'n/a'), ISNULL(vehicle,'n/a'), 
						ISNULL(manufacturer,'n/a')
					FROM   deleted;
	DECLARE curIns CURSOR LOCAL FOR 
					SELECT idFlotte, ISNULL(protocol,'n/a'), idSystem, 
						ISNULL(ecuHardware,'n/a'), ISNULL(ecuType,'n/a'), ISNULL(engineProject,'n/a'), 
						displacement, ISNULL(emissionConcept,'n/a'), ISNULL(platform,'n/a'), 
						ISNULL(power,'n/a'), ISNULL(gearbox,'n/a'), ISNULL(vehicle,'n/a'), 
						ISNULL(manufacturer,'n/a')
					FROM   inserted;
									
    DECLARE @vIdFlotteAnt INT, @vProtocolAnt VARCHAR(30), @vIdSystemAnt INT, @vEcuHardwareAnt VARCHAR(30), 
			@vEcuTypeAnt VARCHAR(30), @vEngineProjectAnt VARCHAR(30), @vDisplacementAnt NUMERIC, 
			@vEmissionConceptAnt VARCHAR(30), @vPlatformAnt VARCHAR(30), @vPowerAnt VARCHAR(30), 
			@vGearboxAnt VARCHAR(30), @vVehicleAnt VARCHAR(30), @vManufacturerAnt VARCHAR(30), 
			
								
	DECLARE @vIdFlotteNuevo INT, @vProtocolNuevo VARCHAR(30), @vIdSystemNuevo INT, @vEcuHardwareNuevo VARCHAR(30), 
			@vEcuTypeNuevo VARCHAR(30), @vEngineProjectNuevo VARCHAR(30), @vDisplacementNuevo NUMERIC, 
			@vEmissionConceptNuevo VARCHAR(30), @vPlatformNuevo VARCHAR(30), @vPowerNuevo VARCHAR(30), 
			@vGearboxNuevo VARCHAR(30), @vVehicleNuevo VARCHAR(30), @vManufacturerNuevo VARCHAR(30), 
			
	
    DECLARE @vAccion AS VARCHAR(4000)

    OPEN curDel;
    OPEN curIns;
    
    FETCH curDel INTO @vIdFlotteAnt, @vProtocolAnt, @vIdSystemAnt, @vEcuHardwareAnt, 
			@vEcuTypeAnt, @vEngineProjectAnt, @vDisplacementAnt, 
			@vEmissionConceptAnt, @vPlatformAnt, @vPowerAnt, 
			@vGearboxAnt, @vVehicleAnt, @vManufacturerAnt, 
			
						
	FETCH curIns INTO @vIdFlotteNuevo, @vProtocolNuevo, @vIdSystemNuevo, @vEcuHardwareNuevo, 
			@vEcuTypeNuevo, @vEngineProjectNuevo, @vDisplacementNuevo, 
			@vEmissionConceptNuevo, @vPlatformNuevo, @vPowerNuevo, 
			@vGearboxNuevo, @vVehicleNuevo, @vManufacturerNuevo, 
			
    
	SET @vAccion='';
	
    IF @vIdFlotteAnt <> @vIdFlotteNuevo
    BEGIN
	  SET @vAccion= @vAccion + 'IdFlotte: ' + CONVERT(VARCHAR,@vIdFlotteAnt) + ' -> ' + CONVERT(VARCHAR,@vIdFlotteNuevo) + '; '
    END
    IF @vProtocolAnt <> @vProtocolNuevo
    BEGIN
	  SET @vAccion= @vAccion + 'Protocol: ' + @vProtocolAnt + ' -> ' + @vProtocolNuevo + '; '
    END
    IF @vIdSystemAnt <> @vIdSystemNuevo
    BEGIN
	  SET @vAccion= @vAccion + 'IdSystem: ' + CONVERT(VARCHAR,@vIdSystemAnt) + ' -> ' + CONVERT(VARCHAR,@vIdSystemNuevo) + '; '
    END
    IF @vEcuHardwareAnt <> @vEcuHardwareNuevo
    BEGIN
	  SET @vAccion= @vAccion + 'EcuHardware: ' + @vEcuHardwareAnt + ' -> ' + @vEcuHardwareNuevo + '; '
    END
    IF @vEcuTypeAnt <> @vEcuTypeNuevo
    BEGIN
	  SET @vAccion= @vAccion + 'EcuType: ' + @vEcuTypeAnt + ' -> ' + @vEcuTypeNuevo + '; '
    END
    IF @vEngineProjectAnt <> @vEngineProjectNuevo
    BEGIN
	  SET @vAccion= @vAccion + 'EngineProject: ' + @vEngineProjectAnt + ' -> ' + @vEngineProjectNuevo + '; '
    END
    IF @vDisplacementAnt <> @vDisplacementNuevo
    BEGIN
	  SET @vAccion= @vAccion + 'Displacement: ' + CONVERT(VARCHAR,@vDisplacementAnt) + ' -> ' + CONVERT(VARCHAR,@vDisplacementNuevo) + '; '
    END
    IF @vEmissionConceptAnt <> @vEmissionConceptNuevo
    BEGIN
	  SET @vAccion= @vAccion + 'EmissionConcept: ' + @vEmissionConceptAnt + ' -> ' + @vEmissionConceptNuevo + '; '
    END
    IF @vPlatformAnt <> @vPlatformNuevo
    BEGIN
	  SET @vAccion= @vAccion + 'Platform: ' + @vPlatformAnt + ' -> ' + @vPlatformNuevo + '; '
    END
    IF @vPowerAnt <> @vPowerNuevo
    BEGIN
	  SET @vAccion= @vAccion + 'Power: ' + @vPowerAnt + ' -> ' + @vPowerNuevo + '; '
    END
    IF @vGearboxAnt <> @vGearboxNuevo
    BEGIN
	  SET @vAccion= @vAccion + 'Gearbox: ' + @vGearboxAnt + ' -> ' + @vGearboxNuevo + '; '
    END
    IF @vVehicleAnt <> @vVehicleNuevo
    BEGIN
	  SET @vAccion= @vAccion + 'Vehicle: ' + @vVehicleAnt + ' -> ' + @vVehicleNuevo + '; '
    END
    IF @vManufacturerAnt <> @vManufacturerNuevo
    BEGIN
	  SET @vAccion= @vAccion + 'Manufacturer: ' + @vManufacturerAnt + ' -> ' + @vManufacturerNuevo + '; '
    END

	
	INSERT obd.LOGMOVIMIENTOS (tabla, idReg, fecha, 
							   user_Id, accion)
    VALUES ('Flotte', @vIdReg, GETDATE(), 
			@vUser_Id, @vAccion)
    
END
GO
